package com.utils.spring;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * <B>功能描述:</B><br>
 * 一次初始化本地容器，保障各模块得到本地容器唯一，节省JVM。<br>
 * 适合使用模块:JUNIT测试，以及一些UTIL,TOOLS,COMMONS,HELPER等工具类<br>
 * <B>创建时间:</B> Jan 12, 2008<br>
 * <B>创建日期:</B> 9:53:32 AM<br>
 *
 */
public abstract class LocalContextBeanFactory {
	private static Resource resource;

	private static BeanFactory beanFactory;
	
	private static Log logger;

	static {
		resource = new ClassPathResource("/spring-base.xml");
		beanFactory = new XmlBeanFactory(resource);
		logger= LogFactory.getLog(LocalContextBeanFactory.class);
	}

	/**
	 * 
	 * @param beanName在容器中的BEANID<br>
	 * @return bean实例<br>
	 */
	public static Object getBean(String beanName) {
		try{
			Object bean=beanFactory.getBean(beanName);
			logger.info(bean);
			return bean;
		}catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}
}
